$(function(){
    var cykmdmys = {
        grid:$("#cykmdmys_grid"),
        gridOptions:{
            url:$.webPath() + "/cwgl/kmys/cykmdmys/cykmdmysList",
//            updateUrl:$.webPath() +'/cwgl/kmys/cykmdmys/save',
            toolbar:'#toolbar_cykmdmys',
            fitColumns:true,
            fit:true,
            rownumbers:true,
            pageNumber: 1,
            pagination:true,
            singleSelect:true,
            columns:[
                [
                    {field:'codeId',title:'CodeID',width:50},
                    {field:'gsjgJc',title:'公司',width:100},
                    {field:'cykmdmys.jqcwkmmc',title:"财务科目名称",editor:{type:'text'},width:150},
                    {field:'cykmdmys.sl',title:"是否税率",editor:{type:'combobox',
                        options:{data:[{text:'是',value:'1'},{text:'否',value:'0'}],panelHeight:60}},width:50,formatter:function(value,row,index){
                        if(value == 1) return '是';
                        else return '否';
                    }},
                    {field:'cykmdmys.kmdm',title:"科目代码",editor:{type:'text'},width:90},
                    {field:'cz',title:'操作',formatter:function(value,row,index){
                        if(value && value == 'save')
                            return '<a index="'+index+'" name="save" href="javascript:void(0)" onclick="save_cykmdmys('+index+')"></a>'
                        return '<a index="'+index+'" name="edit" href="javascript:void(0)" onclick="edit_cykmdmys('+index+')"></a>'
                    },width:30,align:'center'}
                ]
            ],
            onLoadSuccess:function(){
                $("#cykmdmys_bmsz").hide();
                $("a[name='edit']").linkbutton({
                    plain:true,
                    iconCls:'icon-edit'
                });
                $("a[name='save']").linkbutton({
                    plain:true,
                    iconCls:'icon-save'
                });
            },
            onBeforeEdit:function(rowIndex, rowData){
                cykmdmys.grid.edatagrid("updateRow",{
                    index: rowIndex,
                    row:$.extend({},rowData,{cz:'save'})

                });
                $("a[name='save']").linkbutton({
                    plain:true,
                    iconCls:'icon-save'
                });
            },
            onAfterEdit: function (index, row) {
                var url = $.webPath() +'/cwgl/kmys/cykmdmys/save_cykmdmys';
                $.post(url, row, function (data) {
                    if(data.statusCode == 200){
                        cykmdmys.grid.datagrid('updateRow', {
                            index: index,
                            row:$.extend({},row,{cz:'edit'})
                        });
                        $("a[name='edit']").linkbutton({
                            plain:true,
                            iconCls:'icon-edit'
                        });
                    } else {
                        $.messager.alert('info',data.message);
                        cykmdmys.grid.edatagrid("beginEdit",index);
                    }
                }, 'json');
            },
            onSelect:function(rowIndex, rowData){
                if(rowData.codeId <= 870 && rowData.codeId >= 800){
                    $("#cykmdmys_bmsz").show();
                } else {
                    $("#cykmdmys_bmsz").hide();
                }
            }
        },
        search:function(){
            cykmdmys.grid.datagrid("load",{
                gsId:$("#cykmdmys_gs").combobox("getValue") || '',
                kmdm:$("#cykmdmys_kmdm").val() || ''
//                kmdm:$("#cykmdmys_kmdm").val() || ''
            });
        }
    }
    cykmdmys.grid.edatagrid(cykmdmys.gridOptions);
//    $("cykmdmys_drwin").window(cykmdmys.winOption);

    $("#cykmdmys_gs").gsjgtree(1);

    $("#cykmdmys_dr").click(function(){
        var win = $.window({
            url:$.webPath()+'/cwgl/kmys/cykmdmys/cykmdmys_drcontent',
            title:"常用科目代码导入",
            iconCls:'icon-save',
            modal:true,
            closed:false,
            collapsible:false,
            minimizable:false,
            maximizable:false,
            buttons:[
                {
                    text:'导入',
                    handler:function () {
                        $('#cykmdmys_drForm').form('submit', {
                            onSubmit:function(){
                                if (!$('#cykmdmys-uploadify').val()){
                                    $.messager.alert("提示","请选择文件！","info");
                                    return false;
                                }
                                if(!/\.xls$/.test($('#cykmdmys-uploadify').val())){
                                    $.messager.alert("提示","请选择*.xls文件！","info");
                                    return false;
                                }
                                if(!$('#cykmdmys_drForm').form("validate") || !$('#cykmdmys-uploadify').val()){
                                    return false;
                                } else {
                                    $(win).mask();
                                }

                            },
                            success:function (data) {
                                $(win).mask('hide');
                                var json = $.parseJSON(data);
                                if (json.statusCode == 200) {
                                    $(win).window('close');
                                    $('#cykmdmys_grid').datagrid('reload');
                                    $.messager.show({
                                        title:'提示',
                                        msg:json.message,
                                        timeout:3000,
                                        showType:'slide'
                                    });
                                } else
                                    $.messager.alert('提示', json.message, 'error');
                            }
                        });
                    }
                }
            ]
        });
    });
    $("#btn_cykmdmys_search").click(function(){
        cykmdmys.search();
    });
    $("#cykmdmys_bmsz").click(function(){
        var select = $("#cykmdmys_grid").datagrid("getSelected");
        if(!select){
            $.messager.alert("警告","请选中记录!","warning");
            return;
        }
        var codeId = select.codeId;
        if (codeId) {
            cykmbmszwin = $.window({
                title:'常用科目映射编码设置',
                width:750,
                height:450,
                cache:false,
                url:$.webPath() + '/cwgl/kmys/cykmdmys/cykmdmys_bmszcontent?codeId=' + codeId,
                modal:true,
                buttons:[
                    {
                        text:'保存',
                        handler:function(){
                            $("#cykm-bm-save").click();
                        }
                    }
                ]
            });
        }
    });
});
function save_cykmdmys(index){
    $("#cykmdmys_grid").edatagrid("endEdit",index);
}
function edit_cykmdmys(index){
    $("#cykmdmys_grid").edatagrid("beginEdit",index);
}